<?php

class PicsAction extends SCAction {
    public function plist($cid=-1){
	$picsclass = M('pics_index');
	$picsclass=$picsclass->select();
	if($cid<0){
	   $cid=$picsclass[0]['id'];
	};
	$pics = M('pics');
	import('ORG.Util.Page');
	$count=$pics->where('pid='.$cid)->count();
	$Page= new Page($count,1);// 实例化分页类 传入总记录数和每页显示的记录数
//	$Page->setConfig('theme',"%upPage% %nowPage% %downPage%");
	$show= $Page->show();// 分页显示输出
	$this->pics=$pics->where('pid='.$cid)->order('datetime')->limit($Page->firstRow.','.$Page->listRows)->select();
	//$this->pro=$pro->where('pid='.$cid)->select();
	$this->page=$show;
	$this->picsclass=$picsclass;
    $this->display();
	}	
    public function edit($id=-1){
	$pro = M('pics_index');
	$this->proclass=$pro->select();
	$pro = M('pics');
	$this->pro=$pro->find($id);
	$this->cid=$this->pro['pid'];
    $this->display();
	}	
	public function update(){
	$Form   =   D('pics');
    if($Form->create()) {
	  //  $Form->img = $info[0]['savename']; 
        $result =   $Form->save();
        if($result) {
            $this->success('操作成功！');
        }else{
            $this->error('写入错误！');
        }
    }else{
        $this->error($Form->getError());
    }
	}
	public function add($cid=-1){
	$pro = M('pics_index');
	$this->picsclass=$pro->select();
	if(!$this->picsclass){
	   $this->error('至少得有一个图片分类，即将进入添加图片分类页','/admin2/Picsc/plist');
	}
	if($cid<0){
	   $cid=$this->picsclass[0]['id'];
	};
	$this->cid=$cid;
    $this->display();	
	}
	public function insert(){
import('ORG.Net.UploadFile');
$upload = new UploadFile();// 实例化上传类
$upload->maxSize  = 3145728 ;// 设置附件上传大小
$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->savePath =  $_SERVER['DOCUMENT_ROOT'].'/weiw/Public/pic/img/';// 设置附件上传目录
if(!$upload->upload()) {// 上传错误提示错误信息
$this->error($upload->getErrorMsg());
}else{// 上传成功 获取上传文件信息
$info =  $upload->getUploadFileInfo();
}
        $Form   =   D('Pics');
        if($Form->create()) {
		$Form->img = $info[0]['savename'];
		$picsc=M('pics_index');
		$vsize=$picsc->find($Form->pid);
		import('ORG.Util.Image');
		$Image = new Image();
		$Image->thumb($_SERVER['DOCUMENT_ROOT'].'/weiw/Public/pic/img/'.$Form->img,$_SERVER['DOCUMENT_ROOT'].'/weiw/Public/pic/vimg/'.$Form->img,NULL,$vsize["width"],$vsize["height"]);
		//$this->makeThumbnail($_SERVER['DOCUMENT_ROOT'].'/weiw/Public/pic/img/'.$Form->img,$_SERVER['DOCUMENT_ROOT'].'/weiw/Public/pic/vimg/'.$Form->img,$vsize["width"],$vsize["height"]);
            $result =   $Form->add();
            if($result) {
                $this->success('操作成功！');
            }else{
                $this->error('写入错误！');
            }
        }else{
            $this->error($Form->getError());
        }
    }
	public function delpro($id=-1){
    $Form   =   M('pics');

    $byunlink=$Form->find($id);
	$bydelfile=$_SERVER['DOCUMENT_ROOT']."/weiw/Public/pic/img/".$byunlink['img'];
	$bydelfilev=$_SERVER['DOCUMENT_ROOT']."/weiw/Public/pic/vimg/".$byunlink['img'];
	unlink($bydelfile);
	unlink($bydelfilev);
    	if($Form->delete($id)) {
        	$this->success('删除图片数据id:'.$id.',图片文件:'.$byunlink['img'].'成功');// 模板变量赋值
    	}else{
        	$this->error('图片数据库数据删除错误！');
    	}

}

}
?>
